package cz.muni.fi.pa165.library.client.gui.swing;

import cz.muni.fi.pa165.library.api.BookService;
import cz.muni.fi.pa165.library.api.ReaderService;
import java.awt.Dimension;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.xml.ws.soap.SOAPFaultException;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author Equo
 */
public class LibraryMainFrame extends JFrame {

    public static int FRAME_WIDTH = 800;
    public static int FRAME_HEIGHT = 500;
    
    private BookService bookWebService;
    private ReaderService readerWebService;
    
    private LibraryMenuBar libraryMenu;
    private JTabbedPane mainTabPane;
    private BookPanel bookPanel;
    private ReaderPanel readerPanel;

    public LibraryMainFrame() {
        try {
            ApplicationContext appContext = new ClassPathXmlApplicationContext(
                    "classpath:META-INF/clientApplicationContext.xml");

            bookWebService = (BookService) appContext.getBean("bookClient");
            readerWebService = (ReaderService) appContext.getBean("readerClient");

            // Authentication of book web service
            Client bookClient = ClientProxy.getClient(bookWebService);
            Endpoint bookEndPoint = bookClient.getEndpoint();
            Map<String, Object> bookProps = new HashMap<String, Object>();
            bookProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
            bookProps.put(WSHandlerConstants.USER, "soap");
            bookProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_DIGEST);
            bookProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, PasswordCallback.class.getName());
            WSS4JOutInterceptor wssBookOut = new WSS4JOutInterceptor(bookProps);
            bookEndPoint.getOutInterceptors().add(wssBookOut);

            // Authentication of reader web service
            Client readerClient = ClientProxy.getClient(readerWebService);
            Endpoint readerEndPoint = readerClient.getEndpoint();
            Map<String, Object> readerProps = new HashMap<String, Object>();
            readerProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
            readerProps.put(WSHandlerConstants.USER, "soap");
            readerProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_DIGEST);
            readerProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, PasswordCallback.class.getName());
            WSS4JOutInterceptor wssReaderOut = new WSS4JOutInterceptor(readerProps);
            readerEndPoint.getOutInterceptors().add(wssReaderOut);

            setTitle("Library");

            libraryMenu = new LibraryMenuBar();
            setJMenuBar(libraryMenu);

            initTabbedPane();
            add(mainTabPane);

            setSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
            setLocation(Utils.getPointToCenterApplication(FRAME_WIDTH, FRAME_HEIGHT));
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);

        } catch (SOAPFaultException ex) {
            JOptionPane.showMessageDialog(this, "Cannot connect to server.\nAuthentication problem.",
                    "Oops!", JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Cannot connect to server.\nProblem with connection or server is down.",
                    "Oops!", JOptionPane.ERROR_MESSAGE);
            dispose();
            System.exit(0);
        }
    }

    private void initTabbedPane() {
        mainTabPane = new JTabbedPane();
        mainTabPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

        bookPanel = new BookPanel(bookWebService);
        readerPanel = new ReaderPanel(readerWebService);

        mainTabPane.addTab("Book", bookPanel);
        mainTabPane.addTab("Reader", readerPanel);
    }
}